package com.xln.furns.service.impl;

import com.xln.furns.dao.FurnDAO;
import com.xln.furns.dao.OrderDAO;
import com.xln.furns.dao.OrderItemDAO;
import com.xln.furns.dao.impl.FurnDAOimpl;
import com.xln.furns.dao.impl.OrderDAOimpl;
import com.xln.furns.dao.impl.OrderItemDAOimpl;
import com.xln.furns.entity.*;
import com.xln.furns.service.OrderService;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class OrderServiceimpl implements OrderService {

    OrderDAO orderDAO = new OrderDAOimpl();
    OrderItemDAO orderItemDAO = new OrderItemDAOimpl();
    FurnDAO furnDAO = new FurnDAOimpl();
    @Override
    public String saveOrder(Cart cart, Integer memberId) {
        String orderId = System.currentTimeMillis()+""+memberId;
        orderDAO.saveOrder(new Order(orderId,new Date(),cart.getallTotalPrice(),0,memberId));
        for (Map.Entry<Integer, CartItem> entry : cart.getItems().entrySet()){
            CartItem item = entry.getValue();
            OrderItem orderItem = new OrderItem(null,item.getName(),item.getPrice(),item.getCount(),item.getTotalPrice(),orderId);
            orderItemDAO.saveOrderItem(orderItem);

            Furn furn = furnDAO.queryFurnById(item.getId());
            furn.setSales(furn.getSales()+item.getCount());
            furn.setStock(furn.getStock()-item.getCount());
            furnDAO.update(furn);
        }

        cart.clearCart();
        return orderId;
    }

    @Override
    public Page<Order> queryOrderList(Integer pageNo, Integer pageSize,int memberId) {

        Page<Order> page = new Page<>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        Integer pageTotalConut = orderDAO.getTotalRowOrder(memberId)/pageSize;
        page.setTotalRow(orderDAO.getTotalRowOrder(memberId));
        if (orderDAO.getTotalRowOrder(memberId)%pageSize > 0){
//            Integer pageTotalConut = furnDAO.getTotalRow()/pageSize;
            page.setPageTotalConut(pageTotalConut+1);
        }else {
//            Integer pageTotalConut = furnDAO.getTotalRow()/pageSize;
            page.setPageTotalConut(pageTotalConut);
        }
        Integer begin = (pageNo - 1) * pageSize;
        List<Order> pageItem = orderDAO.getPageItem(memberId,begin,pageSize);
        page.setItems(pageItem);
        return page;

    }

    @Override
    public Page<OrderItem> queryOrderItemList(Integer pageNo, Integer pageSize, String orderId) {
        Page<OrderItem> page = new Page<>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        Integer pageTotalConut = orderItemDAO.getTotalRowOrderItem(orderId)/pageSize;
        page.setTotalRow(orderItemDAO.getTotalRowOrderItem(orderId));
        if (orderItemDAO.getTotalRowOrderItem(orderId)%pageSize > 0){
//            Integer pageTotalConut = furnDAO.getTotalRow()/pageSize;
            page.setPageTotalConut(pageTotalConut+1);
        }else {
//            Integer pageTotalConut = furnDAO.getTotalRow()/pageSize;
            page.setPageTotalConut(pageTotalConut);
        }
        Integer begin = (pageNo - 1) * pageSize;
        List<OrderItem> pageItem = orderItemDAO.getPageItemPage(orderId,begin,pageSize);
        page.setItems(pageItem);
        return page;

    }

    @Override
    public Integer queryOrderItemTotalCount(String orderId) {
        Integer orderItemCount = 0;
        List<OrderItem> orderItems = orderItemDAO.getPageItem(orderId);
        for (OrderItem orderItem : orderItems) {
           orderItemCount += orderItem.getCount();
        }

        return orderItemCount;
    }


}
